﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace CommonControls.ClassFiles
{
    public static class IsolatedSettings
    {
        public static void save(string keyname, string keyvalue)
        {
            try
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains(keyname))
                    settings[keyname] = keyvalue;
                else
                    settings.Add(keyname, keyvalue);
                settings.Save();
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in save Method In IsolatedSettings file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("setkeyname_save", keyname);
                exNew.Data.Add("setkeyvalue_save", keyvalue);
                throw exNew;
            }
        }
        public static void Save(string keyName, object keyValue)
        {
            try
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                AddOrUpdateKey(settings, keyName, keyValue);
                settings.Save();
            }
            catch (Exception ex)
            {
                string excepmess = "Exception in getBoolValue Method In IsolatedSettings.cs file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
                ex.Data.Add("setkeyname_save", keyName);
                ex.Data.Add("setkeyvalue_save", keyValue);
                Exceptions.SaveExceptionInLocalStorage(excepmess);
            }
        }
        public static void AddOrUpdateKey(IsolatedStorageSettings settings, string keyName, object keyValue)
        {

            if (settings.Contains(keyName))
                settings[keyName] = Convert.ToString(keyValue);
            else
                settings.Add(keyName, Convert.ToString(keyValue));


        }
        public static string getStringValue(string keyname)
        {
            try
            {
                string keyvalue = "";
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains(keyname))
                    keyvalue = settings[keyname].ToString();
                return keyvalue;
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in getStringValue Method In Common file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("getkeyname_getStringValue", keyname);
                throw exNew;
            }
        }

        public static bool getBoolValue(string keyname)
        {
            try
            {
                bool keyvalue = false;
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains(keyname))
                    keyvalue = Convert.ToBoolean(settings[keyname].ToString());
                return keyvalue;
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in getBoolValue Method In Common file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("getkeyname_getBoolValue", keyname);
                throw exNew;
            }
        }

        public static double getDouableValue(string keyname)
        {
            try
            {
                double keyvalue = 0;
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains(keyname))
                    keyvalue = Convert.ToDouble(settings[keyname].ToString());
                return keyvalue;
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in getBoolValue Method In Common file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("getkeyname_getBoolValue", keyname);
                throw exNew;
            }
        }
        public static int getintValue(string keyname)
        {
            try
            {
                int keyvalue = 0;
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains(keyname))
                    keyvalue = Convert.ToInt32(settings[keyname].ToString());
                return keyvalue;
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in getBoolValue Method In Common file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("getkeyname_getBoolValue", keyname);
                throw exNew;
            }
        }
    }
}
